#@layoutT("物品")
#define css()
<style type="text/css">

	/** 树图标样式 */
	body .layui-tree-skin-skin .layui-tree-branch{
		color: #00C5CD;
	}
	/** 隐藏树图标 */
	.layui-tree-branch/*, .layui-tree-leaf ,.layui-xtree-icon-null  */{
		display: none;
	}
	#maingrid {
		border: none;
	}

	.l-accordion-header{
		display:none;
	}
	.div-scroll{
		overflow:auto;
		border:1px solid #DCDCDC;
		min-height: 595px;
		position: relative;
	}
	.l-layout-header {
		position: relative;
		padding-left: 10px;
		color: #183152;
		font-weight: bold;
		height: 30px;
		line-height: 30px;
		background: #E5EFFE;
		overflow: hidden;
		cursor: pointer;
	}
	.cite{
		color:#5FB878;
	}
</style>
#end

#define main()

<div id='container' class="layui-row">
	<div class="layui-col-xs3 div-scroll" position="left" id="accordion1">
		<div class="l-layout-header" onclick="reloadFunc()">物品菜单</div>
		<div title="物品列表" class="l-scroll"style="left: 0px;  top: 0px;">
			<ul id="leftLayout" style="margin-top:0px;margin-bottom:10px;">
			</ul>
		</div>
	</div>

	<div class="layui-col-xs9" position='center'  id="tablelist"
		 style="border:1px solid #DCDCDC;min-height: 595px;">
		<form class="layui-form layui-form-pane f-form" id='searchForm' lay-filter='searchForm' action="javascript:void(0);">
			<div class='layui-row f-index-toolbar'>
				<div class='layui-col-xs12'>
					<div class="layui-btn-group">
						<button style="margin-top: -2px" id='addBtn_2' type="button" class="layui-btn layui-btn-mini layui-btn-normal layui-btn-sm">
							<i class="layui-icon">&#xe608;</i> 添加
						</button>
						<button style="margin-top: -2px" id='deleteBtn_' type="button" class="layui-btn layui-btn-mini layui-btn-normal layui-btn-sm">
							<i class="layui-icon">&#xe640;</i> 删除
						</button>
					</div>
				</div>
			</div>
		</form>
		<table id="maingrid" lay-filter="maingrid"></table>
	</div>

</div>

<!-- 	每行的操作按钮 -->
<script type="text/html" id="bar_maingrid">
	<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
	<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="imgUpload">图片</a>
</script>
#end

#define js()
<!-- 分页表格 -->
<script type="text/javascript">
    var leftOtions,leftNodes,par_id="";
    var projectName="#(peojectName)";

    function setHight(){
        var contentHeight=$(window).height()-5;
        $("#accordion1").css("height",contentHeight);
        $("#tablelist").css("height",contentHeight);
    }

    $(function(){
        queryEntityTree();
        getEntityList();
        setHight();
    })
	
    function getEntityList(){
        gridArgs.title='物品';
        gridArgs.dataId='entity_id';
        gridArgs.deleteUrl='#(path)/erp/info/entity/delete?primaryKey='+gridArgs.dataId;
        gridArgs.updateUrl='#(path)/erp/info/entity/edit?primaryKey=';
        gridArgs.addUrl='#(path)/erp/info/entity/add?par_entity='+par_id;
        gridArgs.gridDivId ='maingrid';
        initGrid({id : 'maingrid'
            ,elem : '#maingrid'
            ,cellMinWidth: 80
            ,cols : [ [
                {title: '主键',field : 'entity_id',width : 35,checkbox : true},
                {title:'序号',type:'numbers',width:35},
                {title: '物品编码', field: 'entity_code',width:180,sort:true },
                {title: '物品名称', field: 'entity_name',width:180,sort:true },
                {title: '上级目录', field: 'par_entity',width:180,sort:true},
                {title: '描述', field: 'entity_desc',width:180},
                {title: '图片', field: 'image',width:80},
                {title: '单位', field: 'unit',width:80},
                {title: '件数', field: 'unit_by',width:60},
                {title: '适用车型', field: 'car_type',width:120},
                {title: '材质', field: 'material',width:80},
                {title: '官方价', field: 'official_price',width:120,sort:true},
                {title: '最低零售价', field: 'retail_price',width:120,sort:true},
                {title: '批发价', field: 'wholesale_price',width:120,sort:true},
                {title: '协议价', field: 'agreement_price',width:120,sort:true},
                {title: '状态', field: 'status',width:80,sort:true},
                {title: '操作',fixed:'right',width : 180,align : 'left',toolbar : '#bar_maingrid'} // 这里的toolbar值是模板元素的选择器
            ] ]
            ,url:"#(path)/erp/info/entity/list?par_id="+par_id
            ,where:{"object":"sys_tree","primaryKey":gridArgs.dataId}
            ,searchForm : 'searchForm'
        },{
			review:review,
			imgUpload:imgUpload
        });
    }


     //查询所有功能菜单并生成树
     function queryEntityTree(){
         leftNodes="";
         $.ajax({
             url:"#(path)/erp/info/entity/entityTree",
             type:"POST",
             data: {
             },
             success:function(data){
                 if(data.length>0){
                     var treeNode=data[0];
                     showLeftTree(treeNode,0,true);
                     leftTree();
                 }
             }
         });
     }

     var nodeNum=0;
     /**
      * spred：true-展开，false-不展开
      */
     function showLeftTree(treeNode,num,spred){
         var id=treeNode.id;
         var text=treeNode.text;
         if(num!=0){
             leftNodes+=",";
         }
         nodeNum++;
         if(nodeNum>2){
             spred=false;
         }
         leftNodes+="{name:'"+text+"',spread:"+spred+",id:'"+id+"',children:[";
         var children=treeNode.children;
         if(children.length>0){
             for(var i=0;i<children.length;i++){
                 showLeftTree(children[i],i,spred);
             }
         }
         leftNodes+="]}";
     }


     function strToJson(str){
         var json = (new Function("return " + str ))();
         return json;
     }

     function reloadFunc(){
         nodeNum=0;
         queryEntityTree();
     }

    var entityId,entityName;
     //物品树
     function leftTree(){
         layui.use('tree',function(){
             var node=[strToJson(leftNodes.replace("undefined",""))];
             leftOtions={
                 elem: '#leftLayout' //传入元素选择器
                 ,skin: 'skin'
                 ,click: function(item){ //点击节点回调
                     console.log("id:"+item.id);
                     entityId = item.id;
                     console.log("name:"+item.name);
                     entityName = item.name;
                     treeClick(item);
                     $("#addBtn_").off("click");//去掉添加按钮重复绑定事件	      			     
                     getEntityList();
                 }
                 ,nodes: node
             }
             $('ul li').remove();
             layui.tree(leftOtions);
         });
     }

     function treeClick(item){
         functionName=item.name;
         par_id=item.id;
         var cite=document.getElementsByTagName('cite');
         var event=window.event||arguments.callee.caller.arguments[0];//消除浏览器差异
         for(var i=0;i<cite.length;i++){
             if(cite[i]==event.srcElement){
                 cite[i].classList.add("cite")
             }else{
                 cite[i].classList.remove("cite")
             }
         }
         if(functionName==projectName){
             reloadFunc();
         }
     }

    var callbackdata = function () {
        var data = {
            entityId: entityId,
            entityName: entityName
        };
        return data;
    }

    function review(obj){
        var data=obj.data;
        var id=data[gridArgs.dataId];
        openDialog("图片预览窗口","#(path)/erp/info/entity/review?id="+id,false,850,500,null);
    }

    function imgUpload(obj){
        debugger;
        var data=obj.data;
        var id=data[gridArgs.dataId];
        openDialog("图片预览窗口","#(path)/erp/info/entity/toImgUpload?entity_id="+id,false,850,500,null);

    }

</script>
#end


#define layuiFunc()
//监听表单操作
    form.on('switch(isStopFilter)', function(obj){
        var id = obj.value;
        var checked=obj.elem.checked;
        var state=0;
        if(!checked){
            state=1
        }

        $.post(
            "#(path)/portal/core/sysFunc/isStop?isJson=true"
            ,param({
                'id' : id
                ,'state':state
            }),function(feedback) {
                if (feedback.success) {
                    layer.tips("修改成功", obj.othis);
                } else {
                    $(".layui-laypage-btn").click();
                    layer.alert(feedback.msg);
                }
            });
    });

    //监听单元格编辑
    table.on('edit(maingrid)', function(obj){
        var value = obj.value //得到修改后的值
            ,data = obj.data //得到所在行所有键值
            ,field = obj.field; //得到字段
        var params="entity."+field+"="+value;

        $.post("#(path)/erp/info/entity/updateFieldValue?"+params
            ,param({
                'entity.id' : data.id
            })
            ,function(feedback){
                $(".layui-laypage-btn").click();
                layer.msg(feedback.msg);
            });
    });

    //是否展开菜单
    form.on('switch(isSpreadFilter)', function(obj){
        var id = obj.value;
        var checked=obj.elem.checked;
        var state=1;
        if(checked){
            state=0
        }

        $.post(
            "#(path)/portal/core/sysFunc/updateFieldValue"
            ,param({
                'sysFunction.id' : id
                ,'sysFunction.spread':state
            }),function(feedback) {
                if (feedback.success) {
                    layer.tips("修改成功", obj.othis);
                } else {
                    $(".layui-laypage-btn").click();
                    layer.alert(feedback.msg);
                }
            });
    });
#end